<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // https://leetcode-cn.com/problems/reverse-string-ii/
        // 花了一点时间，一开始题目没理解对
        var reverseStr = function(s, k) {
            let tmp = [];
            s = s.split('');
            while (s.length > 0) {
                if (s.length < k) {
                    tmp = [...tmp, ...s.reverse()];
                    s = s.slice(2 * k);
                } else if (s.length < 2 * k && s.length > k) {
                    tmp = [...tmp, ...s.slice(0, k).reverse(), ...s.slice(k, 2 * k)];
                    s = s.slice(2 * k);
                } else {
                    tmp = [...tmp, ...s.slice(0, k).reverse(), ...s.slice(k, 2 * k)];
                    s = s.slice(2 * k);
                }
            }
            return tmp.join('');
        };
        console.log(reverseStr("abcdefgh", 3));
    </script>
</body>
</html>